import DecorView from "./DecorView";

const {ccclass, property} = cc._decorator;
/**
 * 一些常驻的悬浮UI. 如状态栏等.不属于某一个窗口.因此需要单独进行管理.
 */

@ccclass
export default class DecorViewManager extends cc.Component {

    public start() {

    }

    public getView<T extends DecorView>(clz: { prototype: T }): T {
        const winName = clz.prototype.winName;
        if (winName) {
            let children = this.node.getComponentsInChildren(clz);
            if (children) {
                for (const comp of children) {
                    if (comp.node.name === winName) {
                        cc.log("找到同类View,", winName);
                        return comp;
                    }
                }
                return null;
            }
        }
        return this.node.getComponentInChildren(clz);
    }

    public addView(decor: DecorView) {
        this.node.addChild(decor.node);

        let widgets = decor.getComponent(cc.Widget);
        if (!widgets) {
            widgets = decor.addComponent(cc.Widget);
        }
        widgets.left = widgets.top = widgets.right = widgets.bottom = 0;
        widgets.isAlignLeft = widgets.isAlignTop = widgets.isAlignRight = widgets.isAlignBottom = true;
        widgets.updateAlignment();

        return;
    }
}
